package com.burstly.lib.component.networkcomponent.burstly.html.ormma;

import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Build.VERSION;
import android.text.TextUtils;
import android.webkit.JavascriptInterface;
import android.widget.SimpleAdapter;
import android.widget.Toast;
import com.burstly.lib.util.LoggerExt;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class OrmmaUtilityController extends OrmmaController
{
  private static final LoggerExt LOG = LoggerExt.getInstance();
  private static final String TAG = "OrmmaUtilityController";
  private OrmmaAssetController mAssetController;
  private OrmmaDisplayController mDisplayController;
  private OrmmaLocationController mLocationController;
  private OrmmaNetworkController mNetworkController;
  private OrmmaSensorController mSensorController;

  public OrmmaUtilityController(OrmmaView paramOrmmaView, Context paramContext)
  {
    super(paramOrmmaView, paramContext);
    this.mAssetController = new OrmmaAssetController(paramOrmmaView, paramContext);
    this.mDisplayController = new OrmmaDisplayController(paramOrmmaView, paramContext);
    this.mLocationController = new OrmmaLocationController(paramOrmmaView, paramContext);
    this.mNetworkController = new OrmmaNetworkController(paramOrmmaView, paramContext);
    this.mSensorController = new OrmmaSensorController(paramOrmmaView, paramContext);
    paramOrmmaView.addJavascriptInterface(this.mAssetController, "ORMMAAssetsControllerBridge");
    paramOrmmaView.addJavascriptInterface(this.mDisplayController, "ORMMADisplayControllerBridge");
    paramOrmmaView.addJavascriptInterface(this.mLocationController, "ORMMALocationControllerBridge");
    paramOrmmaView.addJavascriptInterface(this.mNetworkController, "ORMMANetworkControllerBridge");
    paramOrmmaView.addJavascriptInterface(this.mSensorController, "ORMMASensorControllerBridge");
  }

  private void addCalendarEvent(int paramInt, String paramString1, String paramString2, String paramString3)
  {
    ContentResolver localContentResolver = this.mContext.getContentResolver();
    long l1 = Long.parseLong(paramString1);
    long l2 = l1 + 3600000L;
    ContentValues localContentValues1 = new ContentValues();
    localContentValues1.put("calendar_id", Integer.valueOf(paramInt));
    localContentValues1.put("title", paramString2);
    localContentValues1.put("description", paramString3);
    localContentValues1.put("dtstart", Long.valueOf(l1));
    localContentValues1.put("hasAlarm", Integer.valueOf(1));
    localContentValues1.put("dtend", Long.valueOf(l2));
    Uri localUri;
    ContentValues localContentValues2;
    if (Build.VERSION.SDK_INT == 8)
    {
      localUri = localContentResolver.insert(Uri.parse("content://com.android.calendar/events"), localContentValues1);
      if (localUri != null)
      {
        long l3 = Long.parseLong(localUri.getLastPathSegment());
        localContentValues2 = new ContentValues();
        localContentValues2.put("event_id", Long.valueOf(l3));
        localContentValues2.put("method", Integer.valueOf(1));
        localContentValues2.put("minutes", Integer.valueOf(15));
        if (Build.VERSION.SDK_INT != 8)
          break label228;
        localContentResolver.insert(Uri.parse("content://com.android.calendar/reminders"), localContentValues2);
      }
    }
    while (true)
    {
      Toast.makeText(this.mContext, "Event added to calendar", 0).show();
      return;
      localUri = localContentResolver.insert(Uri.parse("content://calendar/events"), localContentValues1);
      break;
      label228: localContentResolver.insert(Uri.parse("content://calendar/reminders"), localContentValues2);
    }
  }

  private String createTelUrl(String paramString)
  {
    if (TextUtils.isEmpty(paramString))
      return null;
    StringBuilder localStringBuilder = new StringBuilder("tel:");
    localStringBuilder.append(paramString);
    return localStringBuilder.toString();
  }

  private String getSupports()
  {
    StringBuilder localStringBuilder = new StringBuilder("supports: [ 'level-1', 'screen', 'orientation', 'network']");
    LOG.logDebug("OrmmaUtilityController", "getSupports: {0}", new Object[] { localStringBuilder });
    return localStringBuilder.toString();
  }

  public void activate(String paramString)
  {
    LOG.logDebug("OrmmaUtilityController", "activate: {0}", new Object[] { paramString });
    if (paramString.equalsIgnoreCase("networkChange"))
      this.mNetworkController.startNetworkListener();
    do
    {
      return;
      if ((this.mLocationController.allowLocationServices()) && (paramString.equalsIgnoreCase("locationChange")))
      {
        this.mLocationController.startLocationListener();
        return;
      }
      if (paramString.equalsIgnoreCase("shake"))
      {
        this.mSensorController.startShakeListener();
        return;
      }
      if (paramString.equalsIgnoreCase("tiltChange"))
      {
        this.mSensorController.startTiltListener();
        return;
      }
      if (paramString.equalsIgnoreCase("headingChange"))
      {
        this.mSensorController.startHeadingListener();
        return;
      }
    }
    while (!paramString.equalsIgnoreCase("orientationChange"));
    this.mDisplayController.startConfigurationListener();
  }

  @JavascriptInterface
  public void createEvent(final String paramString1, final String paramString2, final String paramString3)
  {
    LOG.logDebug("OrmmaUtilityController", "createEvent: date: {0} title: {1} body: {2}", new Object[] { paramString1, paramString2, paramString3 });
    ContentResolver localContentResolver = this.mContext.getContentResolver();
    String[] arrayOfString = { "_id", "displayName", "_sync_account" };
    if (Build.VERSION.SDK_INT == 8);
    for (Cursor localCursor = localContentResolver.query(Uri.parse("content://com.android.calendar/calendars"), arrayOfString, null, null, null); (localCursor == null) || (!localCursor.moveToFirst()); localCursor = localContentResolver.query(Uri.parse("content://calendar/calendars"), arrayOfString, null, null, null))
    {
      Toast.makeText(this.mContext, "No calendar account found", 1).show();
      if (localCursor != null)
        localCursor.close();
      return;
    }
    if (localCursor.getCount() == 1)
      addCalendarEvent(localCursor.getInt(0), paramString1, paramString2, paramString3);
    while (true)
    {
      localCursor.close();
      return;
      final ArrayList localArrayList = new ArrayList();
      for (int i = 0; ; i++)
      {
        int j = localCursor.getCount();
        if (i >= j)
          break;
        HashMap localHashMap = new HashMap();
        localHashMap.put("ID", localCursor.getString(0));
        localHashMap.put("NAME", localCursor.getString(1));
        localHashMap.put("EMAILID", localCursor.getString(2));
        localArrayList.add(localHashMap);
        localCursor.moveToNext();
      }
      AlertDialog.Builder localBuilder = new AlertDialog.Builder(this.mContext);
      localBuilder.setTitle("Choose Calendar to save event to");
      localBuilder.setSingleChoiceItems(new SimpleAdapter(this.mContext, localArrayList, 17367053, new String[] { "NAME", "EMAILID" }, new int[] { 16908308, 16908309 }), -1, new DialogInterface.OnClickListener()
      {
        public void onClick(DialogInterface paramAnonymousDialogInterface, int paramAnonymousInt)
        {
          Map localMap = (Map)localArrayList.get(paramAnonymousInt);
          OrmmaUtilityController.this.addCalendarEvent(Integer.parseInt((String)localMap.get("ID")), paramString1, paramString2, paramString3);
          paramAnonymousDialogInterface.cancel();
        }
      });
      localBuilder.create().show();
    }
  }

  public void deactivate(String paramString)
  {
    LOG.logDebug("OrmmaUtilityController", "deactivate: {0}", new Object[] { paramString });
    if (paramString.equalsIgnoreCase("networkChange"))
      this.mNetworkController.stopNetworkListener();
    do
    {
      return;
      if (paramString.equalsIgnoreCase("locationChange"))
      {
        this.mLocationController.stopAllListeners();
        return;
      }
      if (paramString.equalsIgnoreCase("shake"))
      {
        this.mSensorController.stopShakeListener();
        return;
      }
      if (paramString.equalsIgnoreCase("tiltChange"))
      {
        this.mSensorController.stopTiltListener();
        return;
      }
      if (paramString.equalsIgnoreCase("headingChange"))
      {
        this.mSensorController.stopHeadingListener();
        return;
      }
    }
    while (!paramString.equalsIgnoreCase("orientationChange"));
    this.mDisplayController.stopConfigurationListener();
  }

  @JavascriptInterface
  public void deleteOldAds()
  {
    this.mAssetController.deleteOldAds();
  }

  public void init(float paramFloat)
  {
    String str = "window.ormmaview.fireChangeEvent({ state: 'default', network: '" + this.mNetworkController.getNetwork() + "'," + " size: " + this.mDisplayController.getSize() + "," + " maxSize: " + this.mDisplayController.getMaxSize() + "," + " screenSize: " + this.mDisplayController.getScreenSize() + "," + " defaultPosition: { x:" + (int)(this.mOrmmaView.getLeft() / paramFloat) + ", y: " + (int)(this.mOrmmaView.getTop() / paramFloat) + ", width: " + (int)(this.mOrmmaView.getWidth() / paramFloat) + ", height: " + (int)(this.mOrmmaView.getHeight() / paramFloat) + " }, orientation:" + this.mDisplayController.getOrientation() + "," + getSupports() + ", placementType:'" + this.mOrmmaView.getPlacementType() + "', viewable: " + this.mDisplayController.isViewable() + "});";
    LOG.logDebug("OrmmaUtilityController", "init: injection: {0}", new Object[] { str });
    this.mOrmmaView.injectJavaScript(str);
  }

  @JavascriptInterface
  public void makeCall(String paramString)
  {
    LOG.logDebug("OrmmaUtilityController", "makeCall: number: {0}", new Object[] { paramString });
    String str = createTelUrl(paramString);
    if (str == null)
    {
      this.mOrmmaView.raiseError("Bad Phone Number", "makeCall");
      return;
    }
    Intent localIntent = new Intent("android.intent.action.CALL", Uri.parse(str));
    localIntent.addFlags(268435456);
    this.mContext.startActivity(localIntent);
  }

  public String prepareWrappedContentString(String paramString1, String paramString2)
  {
    return this.mAssetController.prepareWrappedContentString(paramString1, paramString2);
  }

  public String readStreamAndPrepareWrappedContentString(InputStream paramInputStream, String paramString)
    throws IOException
  {
    return this.mAssetController.readStreamAndPrepareWrappedContentString(paramInputStream, paramString);
  }

  @JavascriptInterface
  public void sendMail(String paramString1, String paramString2, String paramString3)
  {
    LOG.logDebug("OrmmaUtilityController", "sendMail: recipient: {0} subject: {1} body: {2}", new Object[] { paramString1, paramString2, paramString3 });
    Intent localIntent = new Intent("android.intent.action.SEND");
    localIntent.setType("plain/text");
    localIntent.putExtra("android.intent.extra.EMAIL", new String[] { paramString1 });
    localIntent.putExtra("android.intent.extra.SUBJECT", paramString2);
    localIntent.putExtra("android.intent.extra.TEXT", paramString3);
    localIntent.addFlags(268435456);
    this.mContext.startActivity(localIntent);
  }

  @JavascriptInterface
  public void sendSMS(String paramString1, String paramString2)
  {
    LOG.logDebug("OrmmaUtilityController", "sendSMS: recipient: {0} body: {1}", new Object[] { paramString1, paramString2 });
    Intent localIntent = new Intent("android.intent.action.VIEW");
    localIntent.putExtra("address", paramString1);
    localIntent.putExtra("sms_body", paramString2);
    localIntent.setType("vnd.android-dir/mms-sms");
    localIntent.addFlags(268435456);
    this.mContext.startActivity(localIntent);
  }

  @JavascriptInterface
  public void setMaxSize(int paramInt1, int paramInt2)
  {
    this.mDisplayController.setMaxSize(paramInt1, paramInt2);
  }

  @JavascriptInterface
  public void showAlert(String paramString)
  {
    LOG.logError("OrmmaUtilityController", paramString, new Object[0]);
  }

  public void stopAllListeners()
  {
    try
    {
      this.mAssetController.stopAllListeners();
      this.mDisplayController.stopAllListeners();
      this.mLocationController.stopAllListeners();
      this.mNetworkController.stopAllListeners();
      this.mSensorController.stopAllListeners();
      return;
    }
    catch (Exception localException)
    {
      LOG.logThrowable("OrmmaUtilityController", localException);
    }
  }
}

/* Location:           E:\android\Toy Defense 2_dex2jar.jar
 * Qualified Name:     com.burstly.lib.component.networkcomponent.burstly.html.ormma.OrmmaUtilityController
 * JD-Core Version:    0.6.2
 */